<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Exception;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;

class OrderController extends Controller {

            /*
             *查看
             * */
            public function order_find(Request $request){

                if(Session::get('role_id') == 1 ) {
                    $order = DB::table('order')
                        ->select('*')
                        ->where('status',1)
                        ->where('code',1)
                        ->orderBy('id', 'desc')
                        ->paginate(10);
                }else if(Session::get('role_id') == 2 ){
                          if(empty($request->all())) {
                            $order = DB::table('order')
                                ->select('*')
                                ->where('status', 1)
                                ->where('code', 1)
                                ->where('preget', 1)
                                ->where('company_id', Session::get('session_id'))
                                ->orderBy('id', 'desc')
                                ->paginate(10);
                            return view('admin.order.order_find',['order'=>$order,'code'=>1,'preget'=>1]);
                        }else if(isset($request->code)&&isset($request->preget)){
                           $order = DB::table('order')
                                ->select('*')
                                ->where('status', $request->status)
                                ->where('code', $request->code)
                                ->where('preget', $request->preget)
                                ->where('company_id', Session::get('session_id'))
                                ->orderBy('id', 'desc')
                                ->paginate(10);
                            return view('admin.order.order_find',['order'=>$order,'code'=>$request->code,'preget'=>$request->preget]);
                        }
                }else if(Session::get('role_id') == 3 ){
                    $order = DB::table('order')
                        ->select('*')
                        ->where('status',1)
                        ->where('code',1)
                        ->where('company_id',Session::get('company_id'))
                        ->orderBy('id', 'desc')
                        ->paginate(10);
                }

                return view('admin.order.order_find',['order'=>$order]);
            }


            /*司机查看
             * */
            public function driver_find(Request $request){

                if(Session::get('role_id') == 1 ) {
                    $order = DB::table('order')
                                ->select('*')
                                ->where('driver_status', 0)
                                ->where('preget', 0)
                                ->where('traffic_method',0)
                                ->orderBy('id', 'desc')
                                ->paginate(10);
                            return view('admin.order.driver_find',['order'=>$order,'driver_status'=>0,'preget'=>0]);
                }else if(Session::get('role_id') == 2 ){
                        if(empty($request->all())) {
                            $order = DB::table('order')
                                ->select('*')
                                ->where('driver_status', 0)
                                ->where('preget', 0)
                                ->where('traffic_method',0)
                                ->where('company_id', Session::get('session_id'))
                                ->orderBy('id', 'desc')
                                ->paginate(10);
                            return view('admin.order.driver_find',['order'=>$order,'driver_status'=>0,'preget'=>0]);
                        }else if(isset($request->driver_status)){
                           $order = DB::table('order')
                                ->select('*')
                                ->where('driver_status', $request->driver_status)
                                ->where('preget', $request->preget)
                                ->where('traffic_method',0)
                                ->where('company_id', Session::get('session_id'))
                                ->orderBy('id', 'desc')
                                ->paginate(10);
                            return view('admin.order.driver_find',['order'=>$order,'driver_status'=>$request->driver_status,'preget'=>$request->preget]);
                        }
                }else if(Session::get('role_id') == 3 ){
                    $order = DB::table('order')
                                ->select('*')
                                ->where('driver_status', 0)
                                ->where('preget', 0)
                                ->where('traffic_method',0)
                                ->where('company_id', Session::get('session_id'))
                                ->orderBy('id', 'desc')
                                ->paginate(10);
                            return view('admin.order.driver_find',['order'=>$order,'driver_status'=>0,'preget'=>0]);
                }

            }


            /*
             * 分开处理
             * */
            public function find_process($request){


            }


            /*
             *查看
             * */
            public function order_check(){

                if(Session::get('role_id') == 1 ) {
                    $order = DB::table('order')
                        ->select('*')
                        ->where('status',1)
                        ->where('code',1)
                        ->orderBy('id', 'desc')
                        ->paginate(10);
                }else if(Session::get('role_id') == 2 ){
                    $order = DB::table('order')
                        ->select('*')
                        ->where('status',1)
                        ->where('code',1)
                        ->where('company_id',Session::get('session_id'))
                        ->orderBy('id', 'desc')
                        ->paginate(10);
                }else if(Session::get('role_id') == 3 ){
                    $order = DB::table('order')
                        ->select('*')
                        ->where('status',1)
                        ->where('code',1)
                        ->where('company_id',Session::get('company_id'))
                        ->orderBy('id', 'desc')
                        ->paginate(10);
                }
                return view('admin.order.order_check',['order'=>$order]);

            }



            /*
             * 查看货物
             * */
            public function order_product(Request $request){

                $id = $request->id;
                $order = DB::table('order')
                    ->select('order_product_id')
                    ->where('id',$id)->first();
                $order_product_id = explode(',',$order->order_product_id);
                $order_product = DB::table('order_product')
                    ->select('*')->whereIn('id',$order_product_id)->get();

                return view('admin.order.order_product',['product'=>$order_product]);
            }


            /*
             * 司机分配
             * */
            public function order_driver(Request $request){

                $driver = DB::table('driver')
                    ->select('*')
                    ->where('status',0)
                    ->where('recommend_status',0)
                    ->where('work_status',0)->get();
                $driver = json_decode($driver,1);
                foreach ($driver as $key=>$value){
                 $driver[$key]['face_url'] = '/face_url/'.$value['face_url'];
                 $driver[$key]['car_photo'] = '/image/'.$value['car_photo'];
                 $driver[$key]['car_license'] = '/image/'.$value['car_license'];
                 $driver[$key]['person_license'] = '/image/'.$value['person_license'];
             }

                return view('admin.order.order_driver',['driver'=>$driver,'order_id'=>$request->order_id]);

            }


            /*
             * 司机分配处理
             * */
            public function driver_do(Request $request){
                $order_id = $request->order_id;
                $driver_id = $request->driver_id;
                DB::table('order_to_driver')->where('driver_id',$driver_id)->delete();
                DB::table('order_to_driver')->insert(array('order_id'=>$order_id,'driver_id'=>$driver_id));
                DB::table('driver')->where('id',$driver_id)->update(array('recommend_status'=>1));
                echo '<script language="javascript">alert("分配成功");window.location.href="/admin/order/order_driver?order_id='.$order_id.'";</script>';
            }


            /*
             * 业务员修改
             * */
            public function servicer_manager(Request $request){
                $servicer_id = $request->servicer_id;
                $order_id = $request->order_id;

                $servicer_list = DB::table('servicer')
                           ->select('id')
                           ->where('status',0)
                           ->where('company_id',Session::get('session_id'))
                            ->get();
                       $servicer_id = [];
                       foreach ($servicer_list as $key=>$value){
                           $servicer_id[] = $value->id;
                       }

                $servicer =  DB::table('servicer')
                    ->select('*')
                    ->whereIn('id',$servicer_id)->get();

                $servicer = json_decode($servicer,1);

                 foreach ($servicer as $key=>$value){
                     $servicer[$key]['face_url'] = '/face_url/'.$value['face_url'];
                 }

                 return view('admin.order.servicer_manager',['servicer'=>$servicer,'order_id'=>$order_id]);

            }

            /*
             * 业务员更换
             * */
            public function servicer_change(Request $request){

                $servicer_id = $request->servicer_id;
                $order_id = $request->order_id;

                $order = DB::table('order')->select('customer_id','servicer_id','order_product_id')
                    ->where('id',$order_id)->first();
                //查找客户名之类的
                $servicer = DB::table('servicer')
                    ->select('id','name','telephone')
                    ->where('id',$servicer_id)
                    ->first();
                try {
                    //第一步替换客户
                    DB::table('customer')->where('id', $order->customer_id)->update(array('servicer_id' => $servicer_id));
                    //替换商品的servicer_id
                    $order_product_id = explode(',', $order->order_product_id);
                    DB::table('order_product')
                        ->whereIn('id', $order_product_id)
                        ->update(array('servicer_id' => $servicer_id));
                    //替换该订单的合同
                    DB::table('agreement')
                        ->where('order_id', $order_id)
                        ->where('servicer_id', $order->servicer_id)
                        ->update(array('servicer_id' => $servicer_id));
                    //替换订单的
                    DB::table('order')
                        ->where('id', $order_id)
                        ->update(array('servicer_id' => $servicer_id, 'servicer_name' => $servicer->name
                        , 'servicer_phone' => $servicer->telephone, 'order_servicer' => ''));
                    echo '<script language="javascript">alert("更换成功");window.location.href="/admin/order/order_check";</script>';
                }catch (Exception $e){
                    echo '<script language="javascript">alert("更换失败");window.location.href="/admin/order/order_check";</script>';
                }
            }

            /*
             * 参与人管理
             * */
            public function servicer_add_manager(Request $request){
                    $order_id = $request->order_id;
                    $order = DB::table('order')
                        ->select('order_servicer')
                        ->where('id',$order_id)->first();
                    $order_servicer = explode(',',$order->order_servicer);
                    $servicer = DB::table('servicer')
                        ->select('*')
                        ->whereIn('id',$order_servicer)->get();
                    $servicer = json_decode($servicer,1);
                     foreach ($servicer as $key=>$value){
                         $servicer[$key]['face_url'] = '/face_url/'.$value['face_url'];
                     }
                    return view('admin.order.servicer_add_manager',['servicer'=>$servicer,'order_id'=>$order_id]);
            }

            /*
             * 参与人提成
             * */
            public function servicer_add(Request $request){
                    $data = [];
                    foreach ($request->servicer_id as $key=>$value){
                        $data[$key]['servicer_id'] = $request->servicer_id[$key];
                        $data[$key]['percentage'] = $request->percentage[$key];
                        $data[$key]['order_id'] = $request->order_id;
                    }

                    try {
                        DB::table('order_servicer_add')->where('order_id', $request->order_id)->delete();
                        DB::table('order_servicer_add')->insert($data);
                        echo '<script language="javascript">alert("修改成功");window.location.href="/admin/order/servicer_add_manager?order_id='.$request->order_id.'";</script>';
                    }catch (Exception $e){
                        echo '<script language="javascript">alert("修改失败");window.location.href="/admin/order/servicer_add_manager?order_id='.$request->order_id.'";</script>';
                    }
            }



            /*
             * 参与人删除
             * */
            public function servicer_delete(Request $request){
                $servicer_id = $request->servicer_id;
                $order_id = $request->order_id;
                $order = DB::table('order')
                    ->select('order_servicer')
                    ->where('id',$order_id)->first();
                $order_servicer = explode(',',$order->order_servicer);
                foreach ($order_servicer as $key=>$value){
                    if($value == $servicer_id){
                        unset($order_servicer[$key]);
                        DB::table('order_servicer_add')
                            ->where('id',$order_id)
                            ->where('servicer_id',$servicer_id)
                            ->delete();
                    }
                }

                $order_servicer = implode(',',$order_servicer);
                DB::table('order')
                    ->where('id',$order_id)->update(array('order_servicer'=>$order_servicer));
                echo '<script language="javascript">alert("修改失败");window.location.href="/admin/order/servicer_add_manager?order_id='.$request->order_id.'";</script>';
            }


            /*
             * 订单终极处理--结算操作
             * */
            public function order_accounte(Request $request){

                    $order_cost = DB::table('order_cost')
                        ->select('*')->where('order_id',$request->order_id)->first();
                    if(empty($order_cost)){
                        header('Content-Type:text/html;charset=utf-8');
                       echo '<script language="javascript">alert("成本未设置");window.location.href="/admin/order/order_check";</script>';
                       exit;
                    }else{
                        foreach ($order_cost as $key=>$value){
                            if(empty($value)){
                                header('Content-Type:text/html;charset=utf-8');
                                echo '<script language="javascript">alert("成本未设置完成");window.location.href="/admin/order/order_check";</script>';
                                exit;
                            }
                        }
                    }
                        $order = DB::table('order')
                        ->select('id','servicer_id','driver_id',
                            'pay_status','arriver_date','total_sell','product_num',
                            'order_servicer','order_product_id','company_id')
                        ->where('status',1)
                        ->where('code',1)
                        ->where('driver_status',2)
                        ->where('pay_status',1)
                        ->where('id',$request->order_id)
                        ->first();

                        $pay = DB::table('pay')
                                    ->where('order_id',$order->id)
                                    ->orderBy('id','desc')
                                    ->first();

                        if(empty($order)||empty($pay)){
                            header('Content-Type:text/html;charset=utf-8');
                            echo '<script language="javascript">alert("未有付款凭证");window.location.href="/admin/order/order_check";</script>';
                            exit;
                        }
                        $order_servicer = explode(',',$order->order_servicer);
                        if(!empty($order_servicer)&&$order->pay_status==1) {
                            $order_servicer_add = DB::table('order_servicer_add')
                                ->select('percentage')
                                ->where('order_id', $order->id)
                                ->whereIn('servicer_id', $order_servicer)
                                ->get();

                            if (!empty($order_servicer_add)) {
                                foreach ($order_servicer_add as $key => $value) {
                                    $percentage_money = $order->product_num * $value->percentage;
                                    $percentage['create_time'] = time();
                                    $percentage['create_date'] = date('Y-m-d H:i:s', $percentage['create_time']);
                                    $percentage['servicer_id'] = $order->servicer_id;
                                    $percentage['order_id'] = $order->id;
                                    $percentage['percentage_money'] = $percentage_money;
                                    $percentage['type_name'] = '跟单人提成';
                                    $percentage['type'] = 1;
                                    DB::table('order_servicer_percentage')->insert($percentage);
                                    unset($percentage);
                                }

                            }
                            //没有添加跟单员的提成信息

                        } //没有跟单员

                        if($order->pay_status==1){
                                //$order_cost
                                //$order
                                $order_product_id = explode(',',$order->order_product_id);

                                $order_product = DB::table('order_product')
                                    ->select('*')
                                    ->whereIn('id',$order_product_id)
                                    ->first();

                                //下面开始获取相关值
                                $A = $order_product->cost;
                                $B = $order_cost->store_cost;
                                $C = $order_cost->freight_cost * $order_product->product_num;
                                $D = $order_cost->card_cost * $order_product->product_num;
                                $E = $order_cost->building_pecent * $order_product->total_sell;
                                //获取这个开始确定的时间
                                $now_month = date("Y-m-01",time());
                                $now_month = $now_month.'00:00:00';
                                $last_day_month = date("Y-m-d",strtotime("$now_month +1 month -1 day"));
                                $last_day_month = $last_day_month.'00:00:00';
                                //获取到这个月的吨数
                                if ($order_product->pack_method == '袋装' && $order_product->brand == '润丰'||
                                    $order_product->pack_method == '袋装' && $order_product->brand == '华润') {
                                    $month_product_num = DB::table('order')
                                        ->where('pack_method', $order_product->pack_method)
                                        ->where('brand', $order_product->brand)
                                        ->where('driver_get_date', '<', $last_day_month)
                                        ->where('driver_get_date', '>', $now_month)
                                        ->where('status', 1)
                                        ->where('company_id', $order->company_id)->sum('product_num');
                                    if($month_product_num >= 3000){
                                        $F = 15;
                                    }else{
                                        $F = 0;
                                    }
                                }else{
                                    $F = 0;
                                }
                                $p = $order_product->price;
                                $L = $p  * ($A+$B +$C +$D + $E) + $F;
                                //下面开始计算是否按时付款元素
                                $arriver_date = $order->arriver_date;
                                $arriver_time = strtotime($arriver_date);
                                $over_month_time = strtotime("$arriver_time +1 month");
                                $over_time = strtotime("$arriver_time +1 month + $order_cost->cycle");
                                //计算是否超时或者付款类型

                                $pay_time = $pay->create_time;
                                if($pay->pay_method == '预付款' && $pay->create_time < $arriver_time ){
                                      $T = $L * 0.5;
                                }else if($pay->pay_method == '现金' && $pay->create_time < $over_month_time){
                                      $T = $L * 0.5;
                                }else if($pay->create_time < $over_month_time){
                                      $T = $L * 0.2;
                                }else if($pay->create_time > $over_month_time &&$pay->create_time < $over_time){
                                      $T = $L * 0.2;
                                }else if($pay->create_time > $over_month_time &&$pay->create_time > $over_time){
                                      $T = 2;
                                }else{
                                    header('Content-Type:text/html;charset=utf-8');
                                    echo '<script language="javascript">alert("未付款");window.location.href="/admin/order/order_check";</script>';
                                    exit;
                                }
                                    $S = $order_product->product_num;
                                    $percentage_money = $T *$S;
                                    $percentage['create_time'] = time();
                                    $percentage['create_date'] = date('Y-m-d H:i:s', $percentage['create_time']);
                                    $percentage['servicer_id'] = $order->servicer_id;
                                    $percentage['order_id'] = $order->id;
                                    $percentage['percentage_money'] = $percentage_money;
                                    $percentage['type_name'] = '主业务员提成';
                                    $percentage['type'] = 0;
                                    DB::table('order_servicer_percentage')->insert($percentage);

                                   echo '<script language="javascript">alert("结算成功");window.location.href="/admin/order/order_check";</script>';
                        }
            }

            //付款周期
            public function cycle(Request $request){
                $order_id = $request->order_id;
                if(!isset($request->cycle)){
                   $order_cost = DB::table('order_cost')->where('order_id',$order_id)->first();
                       return view('admin.iframe.cycle', ['order_id' => $order_id, 'order_cost' => $order_cost]);
                }else{
                  $order_cost = DB::table('order_cost')
                       ->select('*')
                       ->where('order_id',$order_id)->first();
                  if(empty($order_cost)){
                      DB::table('order_cost')->insert(array('order_id'=>$order_id,'cycle'=>$request->cycle));
                  }else{
                      DB::table('order_cost')->where('order_id',$order_id)->update(array('cycle'=>$request->cycle));
                  }
                   echo '<script language="javascript">alert("确认成功");window.location.href="/admin/order/cycle?order_id='.$request->order_id.'";</script>';
                }
            }


            //付款周期
            public function card_cost(Request $request){

                $order_id = $request->order_id;
                if(!isset($request->card_cost)){
                       $order_cost = DB::table('order_cost')->where('order_id',$order_id)->first();
                       return view('admin.iframe.card_cost', ['order_id' => $order_id, 'order_cost' => $order_cost]);
                }else{
                  $order_cost = DB::table('order_cost')
                       ->select('*')
                       ->where('order_id',$order_id)->first();

                  if(empty($order_cost)){
                      DB::table('order_cost')->insert($request->all());
                  }else{
                      DB::table('order_cost')->where('order_id',$order_id)->update($request->all());
                  }
                   echo '<script language="javascript">alert("确认成功");window.location.href="/admin/order/card_cost?order_id='.$request->order_id.'";</script>';
                }


            }

            //中转库成本
            public function store_cost(Request $request){

                $order_id = $request->order_id;
                if(!isset($request->store_cost)){

                   $order_cost = DB::table('order_cost')->where('order_id',$order_id)->first();
                       return view('admin.iframe.store_cost', ['order_id' => $order_id, 'order_cost' => $order_cost]);
                }else{
                  $order_cost = DB::table('order_cost')
                       ->select('*')
                       ->where('order_id',$order_id)->first();
                  if(empty($order_cost)){
                      DB::table('order_cost')->insert($request->all());
                  }else{

                      DB::table('order_cost')->where('order_id',$order_id)->update($request->all());
                  }
                   echo '<script language="javascript">alert("确认成功");window.location.href="/admin/order/store_cost?order_id='.$request->order_id.'";</script>';
                }
            }
            //运费成本
            public function freight_cost(Request $request){

                $order_id = $request->order_id;
                if(!isset($request->freight_cost)){
                   $order_cost = DB::table('order_cost')->where('order_id',$order_id)->first();
                       return view('admin.iframe.freight_cost', ['order_id' => $order_id, 'order_cost' => $order_cost]);
                }else{
                  $order_cost = DB::table('order_cost')
                       ->select('*')
                       ->where('order_id',$order_id)->first();
                  if(empty($order_cost)){
                      DB::table('order_cost')->insert($request->all());
                  }else{
                      DB::table('order_cost')->where('order_id',$order_id)->update($request->all());
                  }
                   echo '<script language="javascript">alert("确认成功");window.location.href="/admin/order/freight_cost?order_id='.$request->order_id.'";</script>';
                }

            }

            //工地提成
            public function building_pecent(Request $request){

                $order_id = $request->order_id;
                if(!isset($request->building_pecent)){
                   $order_cost = DB::table('order_cost')->where('order_id',$order_id)->first();
                       return view('admin.iframe.building_pecent', ['order_id' => $order_id, 'order_cost' => $order_cost]);
                }else{
                  $order_cost = DB::table('order_cost')
                       ->select('*')
                       ->where('order_id',$order_id)->first();
                  if(empty($order_cost)){
                      DB::table('order_cost')->insert($request->all());
                  }else{
                      DB::table('order_cost')->where('order_id',$order_id)->update($request->all());
                  }
                   echo '<script language="javascript">alert("确认成功");window.location.href="/admin/order/building_pecent?order_id='.$request->order_id.'";</script>';
                }

            }

            //支付凭证
            public function pay_proof(Request $request){

                    $pay = DB::table('pay')
                        ->where('order_id',$request->order_id)
                        ->get();
                    $pay = json_decode($pay,1);
                     foreach ($pay as $key=>$value){
                         $pay[$key]['proof_pic'] = '/pay/'.$value['proof_pic'];
                     }

                     return view('admin/iframe/pay_proof',['pay'=>$pay,'order_id'=>$request->order_id]);

            }

            //到货备注图片
            public function driver_img(Request $request){

                    $driver_remarks = DB::table('driver_remarks')
                        ->where('order_id',$request->order_id)
                        ->get();
                    $driver_remarks = json_decode($driver_remarks,1);
                    foreach ($driver_remarks as $key=>$value){
                      $remarks_pic_id  = explode(',',$value['remarks_pic']);
                      $remarks_pic =  DB::table('remarks_pic')->whereIn('id',$remarks_pic_id)->get();
                      $driver_remarks[$key]['remarks_pic'] = json_decode($remarks_pic,1);
                    }

                    return view('admin/iframe/driver_img',['driver_remarks'=>$driver_remarks]);
            }





}